package com.cs.user.application.controller;

import com.cs.user.application.dtos.PermissionDTO;
import com.cs.user.common.entity.BaseResponse;
import com.cs.user.common.utlis.ResultUtils;
import com.cs.user.domain.bos.PermissionBO;
import com.cs.user.domain.service.PermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    private PermissionDomainService permissionDomainService;

    @PostMapping("/add")
    public BaseResponse<Boolean> addPermission(@RequestBody PermissionDTO permissionDTO) {
        // 输出日志
        log.info("添加权限参数:{}", permissionDTO);
        // 类型转换
        PermissionBO permissionBO = new PermissionBO();
        BeanUtils.copyProperties(permissionDTO, permissionBO);
        // 调用domain层方法
        return ResultUtils.success(permissionDomainService.addPermission(permissionBO));
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deletePermission(@RequestBody PermissionDTO permissionDTO) {
        // 输出日志
        log.info("删除权限参数:{}", permissionDTO);
        // 类型转换
        PermissionBO permissionBO = new PermissionBO();
        BeanUtils.copyProperties(permissionDTO, permissionBO);
        // 调用domain层方法
        return ResultUtils.success(permissionDomainService.deletePermission(permissionBO));
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updatePermission(@RequestBody PermissionDTO permissionDTO) {
        // 输出日志
        log.info("更新权限参数:{}", permissionDTO);
        // 类型转换
        PermissionBO permissionBO = new PermissionBO();
        BeanUtils.copyProperties(permissionDTO, permissionBO);
        // 调用domain层方法
        return ResultUtils.success(permissionDomainService.updatePermission(permissionBO));
    }
}
